package com.yanq.liancar.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.SmallBOrderDetailsActivity;
import com.yanq.liancar.adapter.ShopOrderListAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.BShopGoodsListBean;
import com.yanq.liancar.bean.OrderListBean;
import com.yanq.liancar.bean.SelectUserAllOrdersBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.view.SimplePadding20Decoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BOrderFragment extends BaseLYFragment {

    @BindView(R.id.order_recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout mSmartRefreshLayout;
    private int pages;
    private int page = 1;
    private int auditStatus;
    private String where;
    private boolean isClear=true;
    private ShopOrderListAdapter adapter;
    private List<SelectUserAllOrdersBean.DataBean.ListBean> mList= new ArrayList<>();

    public static BOrderFragment newInstance(int auditStatus,String where) {
        Bundle bundle = new Bundle();
        bundle.putInt("auditStatus", auditStatus);
        bundle.putString("where",where);
        BOrderFragment pageFragment = new BOrderFragment();
        pageFragment.setArguments(bundle);
        return pageFragment;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_orderlist);
        auditStatus = getArguments().getInt("auditStatus");
        where=getArguments().getString("where");
        initRecyclerview();
        initListener();

    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        isClear=true;
        initOkgo();
    }

    private void initRecyclerview() {

        mRecyclerview.setNestedScrollingEnabled(false);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new ShopOrderListAdapter(R.layout.item_small_b_shoporder, mList);
        mRecyclerview.addItemDecoration(new SimplePadding20Decoration(mActivity));
        mRecyclerview.setAdapter(adapter);
//        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Bundle bundle = new Bundle();
//                bundle.putInt("orderId", mList.get(position).getOrderid());
//                bundle.putInt("orderStatus", mList.get(position).getOrder_status());
//                readyGo(SmallBOrderDetailsActivity.class, bundle);
//            }
//        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("orderId", mList.get(position).getOrderid());
                bundle.putInt("orderStatus", mList.get(position).getOrder_status());
                readyGo(SmallBOrderDetailsActivity.class, bundle);
            }
        });
    }

    private void initListener() {
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;

                initOkgo();
                refreshlayout.finishRefresh();
            }
        });
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                if (pages>=page){
                    initOkgo();
                }
                refreshLayout.finishLoadMore();
            }

        });
    }

    private void initOkgo() {
        OkGo.<String>post(Config.s + "llc/api/selectUserAllOrders")
                .tag(this)
                .params("token", UserBean.getbToken(mActivity))
                .params("pageNum",page)
                .params("pageSize",10)
                .params("order_status",auditStatus)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        SelectUserAllOrdersBean bean = gson.fromJson(response.body(), SelectUserAllOrdersBean.class);
                        if (bean.isIsSuccess()) {
                            pages = bean.getData().getPages();
                            if (isClear) {
                                mList.clear();
                            }
                            mList.addAll(bean.getData().getList());
                            adapter.setNewData(mList);
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }

}
